package com.nhjf.service.impl.followThePainting;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nhjf.command.followThePainting.FollowThePaintingCommand;
import com.nhjf.dao.followThePainting.FollowThePaintingDao;
import com.nhjf.dao.followThePainting.followThePaintingDetails.FollowThePaintingDetailsDao;
import com.nhjf.dao.teachers.CoursesTypeDao;
import com.nhjf.dao.teachers.TeachersDao;
import com.nhjf.model.followThePainting.FollowThePainting;
import com.nhjf.model.followThePainting.followThePaintingDetails.FollowThePaintingDetails;
import com.nhjf.model.teachers.Courses;
import com.nhjf.model.teachers.CoursesType;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.followThePainting.FollowThePaintingQo;
import com.nhjf.pojo.qo.teachers.CoursesTypeQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.followThePainting.FollowThePaintingService;
import com.nhjf.service.impl.BaseServiceImpl;
@Transactional
@Service("followThePaintingService")
public class FollowThePaintingServiceImpl extends BaseServiceImpl<FollowThePainting, FollowThePaintingQo, FollowThePaintingDao> implements FollowThePaintingService{
	@Autowired
	private FollowThePaintingDao followThePaintingDao;
	@Autowired
	private FollowThePaintingDetailsDao detailsDao;
	@Autowired
	private CoursesTypeDao coursesTypeDao;
	@Autowired
	private TeachersDao teacherDao;
	@Override
	protected FollowThePaintingDao getDao() {
		// TODO Auto-generated method stub
		return followThePaintingDao;
	}

	@Override
	public ServiceResultDto createFollowThePaintingComment(FollowThePaintingCommand followThePaintingCommand) {
		// TODO Auto-generated method stub
		ServiceResultDto resultDto = new ServiceResultDto();
		FollowThePainting followThePainting=new FollowThePainting();
		if(followThePaintingCommand.getCover()!=null) {
			followThePainting.setCover(followThePaintingCommand.getCover());
		}else {
			resultDto.setSuccess(false);
			resultDto.setResultMessage("covernull");
		}
		if(followThePaintingCommand.getFtsTitle()!=null) {
			followThePainting.setFtsTitle(followThePaintingCommand.getFtsTitle());
		}else {
			resultDto.setSuccess(false);
			resultDto.setResultMessage("ftsTitlenull");
		}
		if(followThePaintingCommand.getTypeOne()!=null) {
			CoursesTypeQo qo=new CoursesTypeQo();
			qo.setCoursesId(Long.parseLong(followThePaintingCommand.getTypeOne()));
			CoursesType coursesType= coursesTypeDao.queryUnique(qo);
			followThePainting.setTypeOne(coursesType);
		}else {
			resultDto.setSuccess(false);
			resultDto.setResultMessage("typeOnenull");
		}
		if(followThePaintingCommand.getTypeTwo()!=null) {
			CoursesTypeQo qo=new CoursesTypeQo();
			qo.setCoursesId(Long.parseLong(followThePaintingCommand.getTypeTwo()));
			CoursesType coursesType= coursesTypeDao.queryUnique(qo);
			followThePainting.setTypeTwo(coursesType);
		}else {
			resultDto.setSuccess(false);
			resultDto.setResultMessage("typeTwonull");
		}
		if(followThePaintingCommand.getTeacherName()!=null) {
			TeachersQo teachersQo=new TeachersQo();
			teachersQo.setUserId(Long.parseLong(followThePaintingCommand.getTeacherName()));
			Teacher teacher =teacherDao.queryUnique(teachersQo);
			followThePainting.setUploader(teacher);
		}
		
		
		followThePainting.setCreationTime(new Date());
		if(followThePaintingCommand.getWidth()!=null) {
			followThePainting.setWidth(followThePaintingCommand.getWidth());
		}else {
			resultDto.setSuccess(false);
			resultDto.setResultMessage("widthnull");
		}
		if(followThePaintingCommand.getHeight()!=null) {
			followThePainting.setHeight(followThePaintingCommand.getHeight());
		}else {
			resultDto.setSuccess(false);
			resultDto.setResultMessage("heightnull");
		}
		List<String> slist=new ArrayList<String>();
		slist=followThePaintingCommand.getImgList();
		for(int i=0;i<slist.size();i++) {
			FollowThePaintingDetails details=new FollowThePaintingDetails();
			if(followThePaintingCommand.getImgList()!=null) {
				details.setDetailPicture(followThePaintingCommand.getImgList().get(i));
			}else {
				resultDto.setSuccess(false);
				resultDto.setResultMessage("imgListnull");
			}
			if(followThePainting.getId()!=null) {
				details.setFollowThePaintingId(followThePainting.getId());
			}else {
				resultDto.setSuccess(false);
				resultDto.setResultMessage("Idnull");
			}
			if(followThePaintingCommand.getPictureDescription()!=null) {
				String[] descriptions=followThePaintingCommand.getPictureDescription().split(",");
				details.setPictureDescription(descriptions[i]);
			}else {
				resultDto.setSuccess(false);
				resultDto.setResultMessage("pictureDescriptionnull");
			}
			if(followThePaintingCommand.getPictureTitle()!=null) {
				String[] titles=followThePaintingCommand.getPictureTitle().split(",");
				details.setPictureTitle(titles[i]);
			}else {
				resultDto.setSuccess(false);
				resultDto.setResultMessage("pictureTitlenull");
			}
			details.setCreationTime(new Date());
			detailsDao.save(details);
			followThePaintingDao.save(followThePainting);
		}
		
		resultDto.setSuccess(true);
		resultDto.setResultMessage("创建成功");
		return resultDto;
	}

}


